﻿/*==========================================================================
 * Project: SpriterBetaPipelineExtension
 * File: SpriterBetaImporter.cs
 * Copyright(C) 2012 Mark Schmelzenbach
 *
 * Basic XML parser for the SpriterBeta format, this only parses a single
 * character - and only understands animations and frames.
 *
 * Not very fault tolerent, it assumes a well-formed XML file
 *
 *==========================================================================
 * Author:
 *    Mark Schmelzenbach <schmelze@gmail.com>
 *==========================================================================*/

using System;
using System.Collections.Generic;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline;

using TImport = SpriterContentPipelineExtension.SpriterData;
using System.Xml.Serialization;
using System.IO;
using System.Threading;

namespace SpriterContentPipelineExtension
{

    [ContentImporter(".SCML", DisplayName = "Spriter Importer", DefaultProcessor = "SpriterBetaProcessor")]
    public class SpriterBetaImporter : ContentImporter<TImport>
    {
        public override TImport Import(string filename, ContentImporterContext context)
        {
            File.SetAttributes(filename,File.GetAttributes(filename) & ~FileAttributes.ReadOnly);
            Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
            SpriterData input = new SpriterData();


            XmlSerializer serializer = new XmlSerializer(typeof(SpriterData));

            FileStream reader = new FileStream(filename, FileMode.Open);

            input = (SpriterData)serializer.Deserialize(reader);
            input.pathtofile = filename;
            reader.Close();

            return input;

        }
    }
}
